package com.simple.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.simple.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.simple.common.annotation.Log;
import com.simple.common.core.controller.BaseController;
import com.simple.common.core.domain.AjaxResult;
import com.simple.common.enums.BusinessType;
import com.simple.project.domain.Blog;
import com.simple.project.service.IBlogService;
import com.simple.common.utils.poi.ExcelUtil;
import com.simple.common.core.page.TableDataInfo;

/**
 * 博客文章，存储文章相关信息Controller
 * 
 * @author simple
 * @date 2020-12-01
 */
@RestController
@RequestMapping("/project/blog")
@Api("博客文章模块")
public class BlogController extends BaseController {

    @Autowired
    private IBlogService iBlogService;

    /**
     * 查询博客文章列表
     */
    @PreAuthorize("@ss.hasPermi('project:blog:list')")
    @GetMapping("/list")
    @ApiOperation(httpMethod = "GET",value = "查询博客文章列表")
    public TableDataInfo list(Blog blog)
    {
        startPage();
        List<Blog> list = iBlogService.list(null);
        return getDataTable(list);
    }

    /**
     * 导出博客文章，存储文章相关信息列表
     */
    @PreAuthorize("@ss.hasPermi('project:blog:export')" )
    @Log(title = "博客文章，存储文章相关信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Blog blog) {
        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<Blog>(blog);
        List<Blog> list = iBlogService.list(lqw);
        ExcelUtil<Blog> util = new ExcelUtil<Blog>(Blog. class);
        return util.exportExcel(list, "blog" );
    }

    /**
     * 获取博客文章，存储文章相关信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:blog:query')" )
    @GetMapping(value = "/{blogId}" )
    public AjaxResult getInfo(@PathVariable("blogId" ) Long blogId) {
        return AjaxResult.success(iBlogService.getById(blogId));
    }

    /**
     * 新增博客文章，存储文章相关信息
     */
    @PreAuthorize("@ss.hasPermi('project:blog:add')" )
    @Log(title = "博客文章，存储文章相关信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Blog blog) {
        return toAjax(iBlogService.save(blog) ? 1 : 0);
    }

    /**
     * 修改博客文章，存储文章相关信息
     */
    @PreAuthorize("@ss.hasPermi('project:blog:edit')" )
    @Log(title = "博客文章，存储文章相关信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Blog blog) {
        return toAjax(iBlogService.updateById(blog) ? 1 : 0);
    }

    /**
     * 删除博客文章，存储文章相关信息
     */
    @PreAuthorize("@ss.hasPermi('project:blog:remove')" )
    @Log(title = "博客文章，存储文章相关信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{blogIds}" )
    public AjaxResult remove(@PathVariable Long[] blogIds) {
        return toAjax(iBlogService.removeByIds(Arrays.asList(blogIds)) ? 1 : 0);
    }
}
